# -*- coding:utf-8 -*-
"""
@Time : 2022/4/23 8:33 PM
@Author: binkuolo
@Des: views home
"""
from fastapi import Request, APIRouter, Form
from fastapi.responses import HTMLResponse

from models.base import User

router = APIRouter()


@router.get("/", tags=["门户首页"], response_class=HTMLResponse)
async def home(request: Request):
    """
    门户首页
    :param request:
    :return:
    """
    return request.app.state.views.TemplateResponse("index.html", {"request": request})

@router.get("/reg", tags=["注册页面"], response_class=HTMLResponse)
async def reg(request: Request):
    """
    门户首页
    :param request:
    :return:
    """
    return request.app.state.views.TemplateResponse("reg.html", {"request": request})


@router.post("/reg/form", tags=["结果页面"], response_class=HTMLResponse)
async def reg_result(request: Request,username:str=Form(...),password:str=Form(...)):
    """
    门户首页
    :param request:
    :return:
    """
    user_list = await User().all()
    print("所有用户",user_list)
    add_user = await User().create(username=username,password=password)
    print("插入后的结果",add_user)
    print("插入的主键",add_user.pk)
    print("插入的主键",add_user.username)
    return request.app.state.views.TemplateResponse("reg_result.html", {"request": request,"username":username,"password":password})